<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="115px">
      <!--<el-form-item label="所属管辖部门id" prop="deptId">-->
        <!--<el-input-->
          <!--v-model="queryParams.deptId"-->
          <!--placeholder="请输入所属管辖部门id"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="矫正对象姓名" prop="userName">-->
        <!--<el-input-->
          <!--v-model="queryParams.userName"-->
          <!--placeholder="请输入矫正对象姓名"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="联系手机" prop="phonenumber">-->
        <!--<el-input-->
          <!--v-model="queryParams.phonenumber"-->
          <!--placeholder="请输入联系手机"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="性别" prop="gender">-->
        <!--<el-input-->
          <!--v-model="queryParams.gender"-->
          <!--placeholder="请输入性别"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="设备Imei号" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请设备Imei号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="身份证号" prop="idCard">-->
        <!--<el-input-->
          <!--v-model="queryParams.idCard"-->
          <!--placeholder="请输入身份证号"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="帐号状态" prop="status">-->
        <!--<el-select v-model="queryParams.status" placeholder="请选择帐号状态" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['deviceunbind:deviceunbind:add']"-->
        <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['deviceunbind:deviceunbind:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->

      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handBind"-->
          <!--v-hasPermi="['devicebind:devicebind:bind']"-->
        <!--&gt;绑定</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['deviceunbind:deviceunbind:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['deviceunbind:deviceunbind:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceunbindList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="矫正对象ID" align="center" prop="correctionuserId" />-->
      <!--<el-table-column label="所属管辖部门id" align="center" prop="deptId" />-->
      <!--<el-table-column label="矫正对象姓名" align="center" prop="userName" />-->
      <!--<el-table-column label="联系手机" align="center" prop="phonenumber" />-->
      <!--<el-table-column label="性别" align="center" prop="gender" />-->
      <el-table-column label="设备Imei" align="center" prop="imei" />
      <!--<el-table-column label="身份证号" align="center" prop="idCard" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <!--<el-table-column label="帐号状态" align="center" prop="status" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handleUpdate(scope.row)"-->
            <!--v-hasPermi="['deviceunbind:deviceunbind:edit']"-->
          <!--&gt;修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handBind(scope.row)"
            v-hasPermi="['devicebind:devicebind:bind']"
          >绑定</el-button>
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-delete"-->
            <!--@click="handleDelete(scope.row)"-->
            <!--v-hasPermi="['deviceunbind:deviceunbind:remove']"-->
          <!--&gt;删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备未绑定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="设备Imei号" prop="imei">
          <el-input v-model="form.imei" placeholder="请输入设备Imei号" />
        </el-form-item>
        <el-form-item label="矫正对象姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入矫正对象姓名" />
        </el-form-item>
<!--        <el-form-item label="矫正对象ID" prop="correctionuserId">-->
<!--          <el-input v-model="form.correctionuserId" placeholder="请输入矫正对象ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="所属管辖部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属管辖部门ID" />
        </el-form-item>


        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>

        <el-form-item label="联系手机" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系手机" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <!--<el-form-item label="所属管辖部门ID" prop="deptId">-->
          <!--<el-input v-model="form.deptId" placeholder="请输入所属管辖部门ID" />-->
        <!--</el-form-item>-->

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!--<el-form-item label="帐号状态">-->
          <!--<el-radio-group v-model="form.status">-->
            <!--<el-radio label="1">请选择字典生成</el-radio>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="删除标志" prop="delFlag">-->
          <!--<el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceunbind, getDeviceunbind, delDeviceunbind, addDeviceunbind, updateDeviceunbind, bind } from "@/api/devicebind/deviceunbind";

export default {
  name: "Deviceunbind",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备未绑定表格数据
      deviceunbindList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        delFlag: '1',       //未绑定设备
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        phonenumber: null,
        gender: null,
        imei: null,
        idCard: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "矫正对象姓名不能为空", trigger: "blur" }
        ],
        imei: [
          { required: true, message: "设备Imei号不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属管辖部门ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备未绑定列表 */
    getList() {
      this.loading = true;
      listDeviceunbind(this.queryParams).then(response => {
        this.deviceunbindList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        correctionuserId: null,
        deptId: null,
        userName: null,
        phonenumber: null,
        gender: null,
        imei: null,
        idCard: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: "0",
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.correctionuserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备未绑定";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const correctionuserId = row.correctionuserId || this.ids
      getDeviceunbind(correctionuserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备未绑定";
      });
    },

    /** 绑定按钮操作 */
    handBind(row) {
      this.reset();
      // const imei = row.imei || this.ids
      this.form = {
        correctionuserId: null,
        deptId: row.deptId,
        userName: null,
        phonenumber: null,
        gender: null,
        imei: row.imei,
        idCard: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: row.remark,
        status: "0",
        delFlag: null
      };
      this.open = true;
      this.title = "设备绑定";

    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.msg("绑定中，请稍后...");
          bind(this.form).then(response => {
            if(response == 0){
              this.msgSuccess("绑定成功");
            }else{
              this.msgError("绑定失败")
            }

            this.open = false;
            this.getList();

          });

          // if (this.form.correctionuserId != null) {
          //   updateDeviceunbind(this.form).then(response => {
          //     this.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addDeviceunbind(this.form).then(response => {
          //     this.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const correctionuserIds = row.correctionuserId || this.ids;
      this.$confirm('是否确认删除设备未绑定编号为"' + correctionuserIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDeviceunbind(correctionuserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('deviceunbind/deviceunbind/export', {
        ...this.queryParams
      }, `deviceunbind_deviceunbind.xlsx`)
    }
  }
};
</script>
